package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.Topiccontent;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author system
 * @since 2024-12-12 11:42:51
 */
@Component
public class TopiccontentSpecifications {

    public static Specification<Topiccontent> queryList(Topiccontent topiccontent) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (topiccontent.getId() != null) {
                predicates.add(builder.equal(root.get("id" ).as(Long.class), topiccontent.getId()));
            }
            if (topiccontent.getMainTitle() != null) {
                predicates.add(builder.like(root.get("mainTitle" ).as(String.class), "%" + topiccontent.getMainTitle() + "%" ));
            }
            if (topiccontent.getSubTitle() != null) {
                predicates.add(builder.like(root.get("subTitle" ).as(String.class), "%" + topiccontent.getSubTitle() + "%" ));
            }
            if (topiccontent.getContent() != null) {
                predicates.add(builder.like(root.get("content" ).as(String.class), "%" + topiccontent.getContent() + "%" ));
            }
            if (topiccontent.getVideoAttachment() != null) {
                predicates.add(builder.like(root.get("videoAttachment" ).as(String.class), "%" + topiccontent.getVideoAttachment() + "%" ));
            }
            if (topiccontent.getTopicId() != null) {
                predicates.add(builder.equal(root.get("topicId" ).as(Long.class), topiccontent.getTopicId()));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}

